package com.zyx.addressBook;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CMBackupActivity extends Activity implements Runnable{
	
	Button doBackup = null;
	Button doResume = null;
	List<ContactMan> ll;
	String jsonStr = null;
	int flag = 0;
	Thread thread = null;
	
	private String filePathStr = "/sdcard/addressBook.txt"; 
	
    public void onCreate(Bundle savedInstanceState) 
    {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.cmbackup);
    	
    	doBackup = (Button)findViewById(R.id.button1);
    	doResume = (Button)findViewById(R.id.button2);
    	doBackup.setOnClickListener(new OnClickListener(){
    		 public void onClick(View v)
    	     { 
    			newTreadstartWork(1);
//    			// CMFileManager.deleteFile(filePathStr);
//		        CMManager cmm = new CMManager(CMBackupActivity.this);
//		        CMBackupActivity.this.ll = cmm.getContacts(); 
//		        CMJsonManager cmjm = new CMJsonManager();
//		        cmjm.setcList(ll);
//		        CMBackupActivity.this.jsonStr = cmjm.CMListToJson();
//		        System.out.println(CMBackupActivity.this.jsonStr);
//		        byte [] buffer = jsonStr.getBytes();
//		        try {
//		        	CMFileManager.writeSDCardFile(filePathStr, buffer);
//				} catch (IOException e) {
//					e.printStackTrace();
//				}     
    	     }
    	});
    	doResume.setOnClickListener(new OnClickListener(){
	   		 public void onClick(View v)
		     {
	   			newTreadstartWork(2);
//	   			 	String str = null; 
//	   			 	try {
//						str = CMFileManager.readSDCardFile(filePathStr);
//					} catch (IOException e) {
//						e.printStackTrace();
//					}
//			        CMManager cmm = new CMManager(CMBackupActivity.this);
//			        CMJsonManager cmjm = new CMJsonManager();
//			        System.out.println(str);
//			        cmjm.setCjson(str);
//			        List<ContactMan> cmList = cmjm.CMListFromJson(); 
//			        Iterator<ContactMan> iterator = cmList.iterator();
//			        while (iterator.hasNext()) {
//			        	ContactMan cm = (ContactMan) iterator.next();
//			        	cmm.writeContact(cm);
//			        }
		     }
    	});
    }

    public void newTreadstartWork(int flag)
    {
    	this.flag = flag;
		thread = new Thread(this);
		thread.start();
    }
    
	@Override
	public void run() {
		if(flag == 1)
		{
			// CMFileManager.deleteFile(filePathStr);
	        CMManager cmm = new CMManager(CMBackupActivity.this);
	        CMBackupActivity.this.ll = cmm.getContacts(); 
	        try {
				Thread.sleep(600L);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
	        CMJsonManager cmjm = new CMJsonManager();
	        try {
				Thread.sleep(600L);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
	        cmjm.setcList(ll);
	        CMBackupActivity.this.jsonStr = cmjm.CMListToJson();
	        try {
				Thread.sleep(600L);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
//	        System.out.println(CMBackupActivity.this.jsonStr);
	        byte [] buffer = jsonStr.getBytes();
	        try {
	        	CMFileManager.writeSDCardFile(filePathStr, buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}     
		}
		else if(flag == 2)
		{
			String str = null; 
			try {
				str = CMFileManager.readSDCardFile(filePathStr);
			} catch (IOException e) {
				e.printStackTrace();
			}
	        CMManager cmm = new CMManager(CMBackupActivity.this);
	        CMJsonManager cmjm = new CMJsonManager();
//	        System.out.println(str);
	        cmjm.setCjson(str);
	        List<ContactMan> cmList = cmjm.CMListFromJson(); 
	        Iterator<ContactMan> iterator = cmList.iterator();
	        while (iterator.hasNext()) {
	        	ContactMan cm = (ContactMan) iterator.next();
	        	cmm.writeContact(cm);
	        }
		}
		
	}
}
